import cv2
from skimage.color import rgb2gray
from skimage.feature import corner_harris, corner_peaks, corner_subpix
from skimage.draw import disk
import numpy as np

def detect_corners(image):
    corners = corner_harris(image)
    coords = corner_peaks(corners, min_distance=5)
    coords_subpix = corner_subpix(image, coords, window_size=13)

    image_corner = np.copy(image)
    for corner in coords_subpix:
        if np.isnan(corner).any():
            continue
        corner = [int(x) for x in corner]
        if 0 <= corner[0] < image_corner.shape[0] and 0 <= corner[1] < image_corner.shape[1]:
            rr, cc = disk((corner[0], corner[1]), 5, shape=image_corner.shape)
            image_corner[rr, cc] = 1

    return (image_corner * 255).astype(np.uint8)


if __name__ == '__main__':
    img = cv2.imread('../images/image.jpg')
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    corner = detect_corners(gray)
    cv2.imshow('ad', corner)
    cv2.waitKey(0)